import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import cart from '@/components/cart'
import item from '@/components/item'
import detail from '@/components/detail'
import mytest from '@/components/mytest'
import test from '@/components/test'
import Register from '@/components/Register'
import Login from '@/components/Login'
import Update from '@/components/Update'
import admin from '@/components/admin'
import addgoods from '@/components/addgoods'
import search from '@/components/search'

import myprofile from '@/components/myprofile'

Vue.use(Router)

var routes = [
        {
          path:'/',
          name:'index',
          component:index
        },
        {
          path:'/cart',
          name:'cart',
          component:cart
        },
        {
          path:'/item',
          name:'item',
          component:item
        },
        {
          path:'/detail',
          name:'detail',
          component:detail
        },
        {
          path:'/mytest',
          name:'mytest',
          component:mytest
        },{
          path:'/test',
          name:'test',
          component:test
        },{
          path:'/register',
          name:'Register',
          component:Register
        },{
          path:'/login',
          name:'Login',
          component:Login
        },{
          path:'/update',
          name:'Update',
          component:Update
        },{
          path:'/myprofile',
          name:'myprofile',
          component:myprofile,
          beforeEnter:(to,from,next)=>{
            if(localStorage.getItem('username')){
              console.log('已经登录')
              next();
            }else{
              console.log('没有登录')
              next('/login')
            }
          }
        },{
          path:'/admin',
          name:'admin',
          component:admin,
          beforeEnter:(to,from,next)=>{
            if(localStorage.getItem('type')==2){
              console.log('满足条件')
              next();
            }else{
              console.log('不满足条件')
              next('/')
            }
          }
        },{
          path:'/addgoods',
          name:'addgoods',
          component:addgoods
        },,{
          path:'/search',
          name:'search',
          component:search
        },
]

export default new Router({
  routes:routes,
  mode:'history'   /*hash*/
})
